﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Publishing.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Macaw.Moss2007.ContentChangesOverviewSolution
{
    public class CDPathMenuButton : ToolBarMenuButton, System.Web.UI.IPostBackEventHandler
    {

        private List<MenuItemTemplate> CDPathsMI = new List<MenuItemTemplate>();

        public CDPathMenuButton()
        {
            this.ID = "CDPathMenuButton";
        }

        protected override void AddMenuItems()
        {
            try
            {
                ContentDeploymentPathCollection cdpaths = ContentDeploymentPath.GetAllPaths();

                foreach (ContentDeploymentPath path in cdpaths)
                {
                    Uri u = new Uri(path.SourceServerUri , path.SourceSiteCollection);
                    using (SPSite pathsite = new SPSite( u.ToString() ))
                    {
                        if (pathsite.ID.Equals(SPContext.Current.Site.ID))
                        {
                            string ID = "Path_" + path.Id.ToString("N");
                            this.AddMenuItem(ID, path.Name,
                                "/_layouts/images/EMPTY.GIF",
                                string.Format("Select the Path \"{0}\" to display changes since last successful Content Deployment via this Path", path.Name),
                                null, "__doPostBack('" + this.UniqueID + "','" + path.Id.ToString("N") + "')");
                        }
                    }                   
                }
            }
            catch
            {
                // this might fail due to "not farm administrator"
            }

            base.AddMenuItems();
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();


            base.MenuControl.MenuFormat = MenuFormat.ArrowAlwaysVisible;
            if (!this.Page.IsPostBack)
            {
                CDPath = Guid.Empty;
            }
            else
            {
                // Restore state
                CDPath = CDPath;
            }
        }

        public Guid CDPath
        {
            get
            {
                EnsureChildControls();
                return (Guid)this.ViewState["CDPath"];
            }
            set
            {
                this.ViewState["CDPath"] = value;
                if (!value.Equals(Guid.Empty))
                {
                    this.MenuControl.CheckedMenuItemIds = "Path_" + value.ToString("N");
                }
                else
                {
                    try
                    {
                        this.MenuControl.CheckedMenuItemIds = null;
                    }
                    catch
                    {
                        // An exception will be thrown if all MenuItems are un-checked.   :-S
                    }
                }
            }
        }


        #region IPostBackEventHandler Members

        void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            Guid eag = new Guid(eventArgument);
            if (CDPath.Equals(eag))
            {
                CDPath = Guid.Empty;
            }
            else
            {
                CDPath = eag;
            }
        }

        #endregion
    }
}
